<?php
class ClientAction extends CI_Controller{
	
	public function __construct(){
		parent::__construct();
		
		//check loggin
		if ($this->session->userdata('LoggedIn')){
			//load defense model
			$this->load->model('ActionModel', 'action', TRUE);
		}else{
			$arr = array('status' => 'fail', 'message' => 'error message here 1');
			echo json_encode($arr);
			exit(0);
		}
	}
	
	public function create_new_bill(){
		if (!isset($_GET['table_id']) || empty($_GET['table_id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$user_id = intval($this->session->userdata('id'));
		$table_id = intval($_GET['table_id']);
		
		$is_free = $this->action->is_table_free($table_id);
		
		if (!$is_free){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
			
		$table_detail = $this->action->table_detail($table_id);
		if ($table_detail->booking_status != null){
			$booking_time = strtotime($table_detail->booking_time);
			$current_time = time();
			
			if (abs($booking_time - $current_time) <= 3600){
				$is_cancel_booking = $this->action->cancel_booking_table($table_id);
				if ($is_cancel_booking == 0){
					$arr = array('status' => 'fail', 'message' => 'error message here');
					echo json_encode($arr);
					return;
				}
			}
		}
		
		$result = $this->action->create_new_bill($table_id, $user_id);
		
		if ($result){
			if (isset($_GET['extra_table']) && !empty($_GET['extra_table'])){
				$extra_table = $_GET['extra_table'];
				$list_table = json_decode($extra_table);
				foreach ($list_table as $table){
					$id = $table->id;
					$is_free = $this->action->is_table_free($id);
					if (!$is_free){
						$arr = array('status' => 'fail', 'message' => 'error message here');
						echo json_encode($arr);
						return;
					}
					$this->action->update_table_status($id, 3, $table_id);
				}
			}	
			$this->action->update_table_status($table_id, 2, null);
			
			$bill = $this->action->bill_detail($result);
			
			$arr = array('status' => 'success', 'info' => $bill);
			
			echo json_encode($arr);
		}else{
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
	}
	
	public function cancel_bill(){
		if (!isset($_GET['table_id']) || empty($_GET['table_id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$table_id = intval($_GET['table_id']);
		
		$count = $this->action->isCanCancelBill($table_id);
		
		if ($count > 0){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$this->db->trans_start();
		
		$this->action->deleteBill($table_id);
		
		$this->action->cancelTableStatus($table_id);
		
		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}else{
			$this->db->trans_commit();
			$arr = array('status' => 'success');
			echo json_encode($arr);
		}
	}
	
	/*public function add_dishes_to_bill(){
		if (!isset($_GET['bill_id']) || empty($_GET['bill_id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$bill_id = $_GET['bill_id'];
		
		$is_add_dish = $this->action->is_exist_bill($bill_id);
		if (!$is_add_dish){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		if (!isset($_GET['list_id']) || empty($_GET['list_id'])){
			$list_dish_id = $_GET['list_id'];
			$list_dish_id = json_decode($list_dish_id);
			
			$this->db->trans_start();
			foreach ($list_dish_id as $dish_id){
				$id = $dish_id->id;
				$this->add_dish_to_bill($bill_id, $bill_id)
			}
			
			if ($this->db->trans_status() === FALSE){
				$this->db->trans_rollback();
				$arr = array('status' => 'fail', 'message' => 'error message here');
				echo json_encode($arr);
				return;
			}else{
				$this->db->trans_commit();
				
				$bill = $this->action->bill_detail($bill_id);
				
				$bill_desciption = $this->action->bill_description_detail($bill_id);
				
				$arr = array('status' => 'success',
							 'bill' => $bill,
							 'bill_description' => $bill_desciption);
				
				echo json_encode($arr);
			}
		}
	}*/
	
	public function get_bill_detail(){
		if (!isset($_GET['bill_id']) || empty($_GET['bill_id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$bill_id = $_GET['bill_id'];
		
		$bill = $this->action->bill_detail($bill_id);
			
		$bill_desciption = $this->action->bill_description_detail($bill_id);
		
		if (!$bill || !$bill_desciption){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$arr = array('status' => 'success',
					 'bill' => $bill,
					 'bill_description' => $bill_desciption);
		
		echo json_encode($arr);
	}
	
	public function add_dish_to_bill(){
		if (!isset($_GET['bill_id']) || empty($_GET['bill_id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$bill_id = $_GET['bill_id'];
		
		$is_add_dish = $this->action->is_exist_bill($bill_id);
		if (!$is_add_dish){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		if (!isset($_GET['info']) || empty($_GET['info'])){
			$arr = array('status' => 'fail', 'message' => 'error message here 2');
			echo json_encode($arr);
			return;
		}
		
		
		
		$add_info = $_GET['info'];
		$dishes_detail = json_decode($add_info);
		
		$this->db->trans_start();
		$current_time = time();
		foreach ($dishes_detail as $dish){		
			$info['bill_id'] = $bill_id;	
			$info['food_id'] = $dish->id;
			$info['number'] = $dish->number;
			if (isset($dish->food_note)){
				$info['food_note'] = $dish->food_note;
			}
			
			$dish_detail = $this->action->dish_detail($dish->id);
			$info['cost'] = $dish_detail->cost;
			$info['date'] = date('y-m-d', $current_time);
			$info['start_time'] = date('y-m-d h-i-s', $current_time);
			$this->action->add_dish_bill($info);
		}
		
		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}else{
			$this->db->trans_commit();
			
			$bill = $this->action->bill_detail($bill_id);
			
			$bill_desciption = $this->action->bill_description_detail($bill_id);
			
			$arr = array('status' => 'success',
						 'bill' => $bill,
						 'bill_description' => $bill_desciption);
			
			echo json_encode($arr);
		}
	}
	
	public function serve_dish(){
		if (!isset($_GET['ids']) || empty($_GET['ids'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$this->db->trans_start();
		$dishes = $_GET['ids'];
		$list_dishes = json_decode($dishes);
		foreach ($list_dishes as $dish){
			$id = $dish->id;
			$result = $this->action->serve_dish($id);
			if ($result == 0){
				$this->db->trans_rollback();
				$arr = array('status' => 'fail', 'message' => 'error message here');
				echo json_encode($arr);
				return;
			}
		}
		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}else{
			$this->db->trans_commit();
			$arr = array('status' => 'success');
			echo json_encode($arr);
		}
	}
	
	public function billing(){
		if (!isset($_GET['bill_id']) || empty($_GET['bill_id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		$bill_id = $_GET['bill_id'];
		
		$is_add_dish = $this->action->is_exist_bill($bill_id);
		if (!$is_add_dish){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$this->db->trans_start();
		
		$this->action->update_bill_description($bill_id);
		
		$bill_desciption = $this->action->bill_description_detail($bill_id);
		
		if ($bill_desciption != false){
			if (isset($_GET['feedback'])){
				$info['feedback'] = $_GET['feedback'];
			}
			$info['total_cost'] = 0;
			foreach ($bill_desciption as $description){
				$info['total_cost'] = $info['total_cost'] + intval($description->number)*intval($description->cost);
			}
			
			$info['end_time'] = date('y-m-d h-i-s', time());
			
			$this->action->update_bill($bill_id, $info);
			
			$bill = $this->action->bill_detail($bill_id);
			
			$this->action->update_table_status($bill->table_id, 1, null);
			
			$this->action->update_extra_table_status($bill->table_id);
			
			$arr = array('status' => 'success',
						 'bill' => $bill,
						 'bill_description' => $bill_desciption);
			
			echo json_encode($arr);
		}else{
			$this->action->delete_bill($bill_id);
			$arr = array('status' => 'cancel');
			echo json_encode($arr);
		}
		
		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}else{
			$this->db->trans_commit();
		}
	}
	
	public function feedback_dish(){
		
		if (!isset($_GET['bill_id']) || empty($_GET['bill_id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$bill_id = $_GET['bill_id'];
		
		$is_add_dish = $this->action->is_exist_bill($bill_id);
		if (!$is_add_dish){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		if (!isset($_GET['info']) || empty($_GET['info'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$info = $_GET['info'];
		$list_dishes = json_decode($info);
		$this->db->trans_start();
		
		foreach ($list_dishes as $dish){
			$id = $dish->id;
			$feedback = $dish->feedback;
			$result = $this->action->feedback_dish($id, $feedback);
			if ($result == 0){
				$this->db->trans_rollback();
				$arr = array('status' => 'fail', 'message' => 'error message here');
				echo json_encode($arr);
				return;
			}
		}
		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}else{
			$this->db->trans_commit();
			$arr = array('status' => 'success');
			echo json_encode($arr);
		}
	}
	
	public function booking_table(){
		if (!isset($_GET['table_id']) || empty($_GET['table_id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$table_id = intval($_GET['table_id']);
		$object['booking_status'] = 4;
		
		if (!isset($_GET['booking_time']) || empty($_GET['booking_time'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$object['booking_time'] = $_GET['booking_time'];
		
		if (isset($_GET['booking_description'])){
			$object['booking_description'] = $_GET['booking_description'];
		}
		
		$affect = $this->action->bookingTable($table_id, $object);
		
		if ($affect > 0){
			$arr = array('status' => 'success');
			echo json_encode($arr);
			return;
		}else{
			$arr = array('status' => 'fail');
			echo json_encode($arr);
			return;
		}
	}
	
	public function get_all_bill_by_user(){
		if (!isset($_GET['user_id']) || empty($_GET['user_id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$user_id = $_GET['user_id'];
		
		$list_bill = $this->action->bill_detail_by_user($user_id);
		
		if (!$list_bill){
			$arr = array('status' => 'fail', 'message' => 'error message here 1');
			echo json_encode($arr);
			return;
		}
		
		$index = 0;
		foreach ($list_bill as $bill) {
			$info[$index] = null;
			$list_bill_description = $this->action->bill_description_detail($bill->_id);
			$table = $this->action->table_detail($bill->table_id);
			$info[$index]["bill"] = $bill;
			if ($list_bill_description == false){
				$info[$index]["bill_description"] = array();
			}else{
				$info[$index]["bill_description"] = $list_bill_description;
			}
			$info[$index]["table"] = $table;
			$index++;
		}
		
		$arr = array('status' => 'success', 'info' => $info);
		echo json_encode($arr);
	}
	
	public function delete_dishes_of_bill(){
		if (!isset($_GET['bill_id']) || empty($_GET['bill_id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$bill_id = $_GET['bill_id'];
		
		$is_add_dish = $this->action->is_exist_bill($bill_id);
		if (!$is_add_dish){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		if (!isset($_GET['info']) || empty($_GET['info'])){
			$arr = array('status' => 'fail', 'message' => 'error message here 2');
			echo json_encode($arr);
			return;
		}
		
		$add_info = $_GET['info'];
		$dishes_detail = json_decode($add_info);
		
		$this->db->trans_start();
		$current_time = time();
		foreach ($dishes_detail as $dish){
			$delete_dish_in_bill = $this->action->delete_dish_bill($bill_id, $dish->id);
			if ($delete_dish_in_bill <= 0){
				$this->db->trans_rollback();
				$arr = array('status' => 'fail', 'message' => 'error message here');
				echo json_encode($arr);
				return;
			}
		}
		
		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}else{
			$this->db->trans_commit();
			
			$bill_desciption = $this->action->bill_description_detail($bill_id);
			
			$arr = array('status' => 'success',
						 'bill_description' => $bill_desciption);
			
			echo json_encode($arr);
		}
	}
	
	public function modify_dishes_of_bill(){
		if (!isset($_GET['bill_id']) || empty($_GET['bill_id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$bill_id = $_GET['bill_id'];
		
		$is_add_dish = $this->action->is_exist_bill($bill_id);
		if (!$is_add_dish){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		if (!isset($_GET['info']) || empty($_GET['info'])){
			$arr = array('status' => 'fail', 'message' => 'error message here 2');
			echo json_encode($arr);
			return;
		}
		
		$add_info = $_GET['info'];
		$dishes_detail = json_decode($add_info);
		
		$this->db->trans_start();
		$current_time = time();
		foreach ($dishes_detail as $dish){
			$info['number'] = $dish->number;
			if (isset($dish->food_note)){
				$info['food_note'] = $dish->food_note;
			}
			$modify_dish_in_bill = $this->action->modify_dish_bill($bill_id, $dish->id, $info);
			if ($modify_dish_in_bill == 0){
				$this->db->trans_rollback();
				$arr = array('status' => 'fail', 'message' => 'error message here');
				echo json_encode($arr);
				return;
			}
		}
		
		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}else{
			$this->db->trans_commit();
			
			$bill_desciption = $this->action->bill_description_detail($bill_id);
			
			$arr = array('status' => 'success',
						 'bill_description' => $bill_desciption);
			
			echo json_encode($arr);
		}
	}
	
	public function serve_dishes(){
		if (!isset($_GET['bill_id']) || empty($_GET['bill_id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$bill_id = $_GET['bill_id'];
		
		$is_add_dish = $this->action->is_exist_bill($bill_id);
		if (!$is_add_dish){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		if (!isset($_GET['info']) || empty($_GET['info'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$add_info = $_GET['info'];
		$list_dishes_id = json_decode($add_info);
		$this->db->trans_start();
		$current_time = new DateTime(time());
		//$current_time = new DateTime(time());
		foreach ($list_dishes_id as $dishes_id){
			$info['serve_time'] = $current_time;
			$serve_dish = $this->action->serve_dishes($bill_id, $dishes_id->id, $info);
			if ($serve_dish == 0){
				$this->db->trans_rollback();
				$arr = array('status' => 'fail', 'message' => 'error message here');
				echo json_encode($arr);
				return;
			}
		}
		
		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}else{
			$this->db->trans_commit();
			
			$bill_desciption = $this->action->bill_description_detail($bill_id);
			
			$arr = array('status' => 'success',
						 'bill_description' => $bill_desciption);
			
			echo json_encode($arr);
		}
	}
	
	public function cancel_booking(){
		if (!isset($_GET['table_id']) || empty($_GET['table_id'])){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}
		
		$table_id = $_GET['table_id'];
		
		$is_cancel_booking = $this->action->cancel_booking_table($table_id);
		
		if ($is_cancel_booking == 0){
			$arr = array('status' => 'fail', 'message' => 'error message here');
			echo json_encode($arr);
			return;
		}else{
			$arr = array('status' => 'success');
			echo json_encode($arr);
			return;
		}
	}
}